import sys
import pickle
import datetime

class Payment:
    def __init__(self, Pay_ID, EID, PID, RID, Date, Desc, Amount, Received):
        self.Pay_ID = Pay_ID
        self.EID = EID
        self.PID = PID
        self.RID = RID
        self.Date = Date
        self.Desc = Desc
        self.Amount = Amount
        self.Received  = Received

def FormatTimeDateToString(DateTimeObj):
    STR = """%s, %s, %s"""%(str(DateTimeObj.year), str(DateTimeObj.month), str(DateTimeObj.day))
    return STR

def FormatStringtoTimeDate(String):
    Numbers = String.split(',')
    return datetime.date(int(Numbers[0]), int(Numbers[1]), int(Numbers[2]))
    

class PaymentTable:
    def __init__(self, SQLConn, dBName = "Payments"):
        self.SQLConn = SQLConn
        self.PaymentsdBName = dBName
        
    def AddPayment(self, EID, PID, RID, Amount, Date, Disc, Recieved = 0):
        self.SQLConn.SendCommand("""
         INSERT INTO %s (Eve_id, Per_Paying, Per_Paid, Amount, Exp_Date, Exp_Discription, Payment_Recieved)
         VALUES ('%d', '%d', '%d', '%f', '%s', '%s', '%d')
       """ % (self.PaymentsdBName, EID, PID, RID, Amount, FormatTimeDateToString(Date), Disc, Recieved))
        
    def RemovePayment(self, Pay_Id):
        self.SQLConn.SendCommand("""
         DELETE FROM %s where Pay_id=%d
         """ % (self.PaymentsdBName, Pay_Id))
        
    
    def UpdatePayment(self, Payment):
        self.SQLConn.SendCommand("""UPDATE %s
                                SET Eve_id = '%d',
                                Per_Paying = '%d',
                                Per_Paid = '%d',
                                Amount = '%f',
                                Exp_Date  = '%s',
                                Exp_Discription = '%s',
                                Payment_Recieved = '%d'
                                WHERE Pay_id = %d
                                """ % (self.PaymentsdBName, Payment.EID, Payment.PID, Payment.RID,
                                       Payment.Amount, FormatTimeDateToString(Payment.Date), Payment.Desc, Payment.Received, Payment.Pay_ID))
        
    def GetPayments(self, Pay_Id = -1, EID=-1, PID = -1, RID = -1, Amount = -1, Date = -1, Disc = -1, Received = -1):
        First = True
        str = """WHERE """
        if Pay_Id != -1:
            if First:
                First = False
                str = str + """Pay_id = '%d' """ % Pay_Id
            else:
                str = str + """AND Pay_id = '%d' """ % Pay_Id
        if EID != -1:
            if First:
                First = False
                str = str + """Eve_id = '%d' """ % EID#FormatTimeDateToString(Date)
            else:
                str = str + """AND Eve_id = '%d' """ % EID#FormatTimeDateToString(Date)
        if PID != -1:
            if First:
                First = False
                str = str + """Per_Paying = '%d' """ % PID#FormatTimeDateToString(Date)
            else:
                str = str + """AND Per_Paying = '%d' """ % PID#FormatTimeDateToString(Date)
        if RID != -1:
            if First:
                First = False
                str = str + """Per_Paid = '%d' """ % RID#FormatTimeDateToString(Date)
            else:
                str = str + """AND Per_Paid = '%d' """ % RID#FormatTimeDateToString(Date)
        if Amount != -1:
            if First:
                First = False
                str = str + """Amount = '%f' """ % Amount
            else:
                str = str + """AND Amount = '%f' """ % Amount
        if Date != -1:
            if First:
                First = False
                str = str + """Exp_Date = '%s' """ % FormatTimeDateToString(Date)
            else:
                str = str + """AND Exp_Date = '%s' """ % FormatTimeDateToString(Date)
        if Disc != -1:
            if First:
                First = False
                str = str + """Exp_Discription = %s """ % Disc
            else:
                str = str + """AND Exp_Discription = '%s' """ % Disc
        if Received != -1:
            if First:
                First = False
                str = str + """Payment_Recieved = %d """ % Received
            else:
                str = str + """AND Payment_Recieved = '%d' """ % Received
        
        self.SQLConn.SendCommand("""SELECT Pay_id, Eve_id, Per_Paying, Per_Paid, Exp_Date, Exp_Discription, Amount, Payment_Recieved FROM %s """%self.PaymentsdBName + str)
        Data = self.SQLConn.FetchAll ()
        Res = {}
    
        for Exp in Data:
            Res[int(Exp[0])] = Payment(int(Exp[0]), int(Exp[1]), int(Exp[2]), int(Exp[3]), FormatStringtoTimeDate(Exp[4]), Exp[5], float(Exp[6]), int(Exp[7]))
                    
        return Res
    


            
    
    
def GenerateTables(SQL):
    print """Generating "Payments" Table"""
    
    SQL.SendCommand ("DROP TABLE IF EXISTS Payments")
    SQL.SendCommand("""CREATE TABLE Payments(
                    Pay_id int unsigned not null auto_increment primary key,
                    Eve_id int,
                    Per_Paying int,
                    Per_Paid int,
                    Amount float,
                    Exp_Date BLOB,
                    Exp_Discription varchar(1000),
                    Payment_Recieved int
                    )
                    """)

    
  
    
        